using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.Security;
using System.Security.Principal;
using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;

namespace Microsoft.ConnectionEngine.Registers.Health.Patient.Business
{
    public class SecurityManager
    {
        public static AspnetMembershipUser CreateUser(AspnetMembershipUser user)
        {
            string payload = common.Utilities.SerializeObject(user, typeof(AspnetMembershipUser));

            common.ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1019, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return (AspnetMembershipUser)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(AspnetMembershipUser)); 
        }

        public static HSDPrincipal ValidateUser(string username, string password)
        {
            string payload = @"<UserCredentials  xmlns=""http://Microsoft.ConnectionEngine/Schemas/ServiceProviderRegister""><UserName>@username</UserName><Password>@password</Password></UserCredentials>";
            payload = payload.Replace(@"@username", username);
            payload = payload.Replace(@"@password", password);

            common.ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.ValidateUser), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return (HSDPrincipal)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(HSDPrincipal));         
        }

        public static string ConstructUserCredentialsXml(HSDPrincipal principal)
        {
            string strUserCredentials = @"<UserCredentials><UserName>@username</UserName><Password>@password</Password></UserCredentials>";
            strUserCredentials = strUserCredentials.Replace(@"@username", principal.HSDIdentity.Name);
            strUserCredentials = strUserCredentials.Replace(@"@password", principal.HSDIdentity.Password);
            return strUserCredentials;
        }

        //Check rights
        public static bool ProviderRights(HSDPrincipal principal, string providerId)
        {
            if (principal.IsInRole(RoleTypes.Administrator) || 
                (principal.IsInRole(RoleTypes.Provider) && 
                    String.Compare(principal.HSDIdentity.ProviderID, providerId, true)==0))
                return true;
            else
                return false;
        }

        public static bool UserInRoles(HSDPrincipal user, String[] roles)
        {
            foreach (String role in roles)
            {
                if (user.IsInRole(role))
                    return true;
            }
            return false;
        }
        public static bool ClientRights(HSDPrincipal principal, string hcuId)
        {
            //TODO: also check if provider and the user belongs to that provider
            // get the user - check the provider list ids
            
            if (principal.IsInRole(RoleTypes.Administrator) ||
                (principal.IsInRole(RoleTypes.Client) && String.Equals(principal.HSDIdentity.HCUID, hcuId, 
                StringComparison.OrdinalIgnoreCase)))
                return true;
            else
                return false;
        }       

    }

    public class SecurityException : Exception
    {

        public SecurityException():base()
        {
        }

        public SecurityException(string msg)
            : base(msg)
        {
        }

        public SecurityException(string msg, Exception inner)
            : base(msg, inner)
        {
        }
        
    }
}
